package swing.frame.whiteboard;


import swing.customComp.AutoWidthTextField;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.geom.Rectangle2D;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class DrawPanel extends JPanel {

    public CopyOnWriteArrayList<String> shapeList = new CopyOnWriteArrayList<>();

    public AutoWidthTextField textField = new AutoWidthTextField();

    public DrawPanel(){
        init();
    }
    JPanel panel=new JPanel() {
        @Override
        public void paint(Graphics g) {
            g2d=(Graphics2D)g;
            super.paint(g2d);
            for(String shapeStr : shapeList){
                Shape shape = new Shape(shapeStr);
                shape.Draw(g2d);
            }
        }
    };
    Graphics2D g2d;


    public void init(){
        //设置流式布局
        this.setLayout(new FlowLayout(FlowLayout.LEFT));
        this.setBackground(Color.GRAY);
        //this.setPreferredSize(new Dimension(1200,800));
        panel.setBackground(Color.white);
        panel.setLayout(null);
        panel.setPreferredSize(new Dimension(1076,800));
//        textField.setBorder(null);
        textField.setFont(new Font("宋体",Font.PLAIN,18));
        textField.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                super.focusLost(e);
                textField.setVisible(false);
            }
        });
        textField.setVisible(false);
        panel.add(textField);
        this.add(panel);
    }

    public synchronized void update(List<String> updateShapeList, boolean isLoad) {
        shapeList.addAll(updateShapeList);
        g2d = (Graphics2D)panel.getGraphics();
        if (!updateShapeList.isEmpty()) {
            if (!isLoad) {
                Shape shape = new Shape(updateShapeList.get(0));
                showDrawUser(shape);
            }
            for (String shapeStr : updateShapeList) {
                Shape shape = new Shape(shapeStr);
                shape.Draw(g2d);
            }
        }
    }

    private void showDrawUser(Shape shape) {
        if (!shape.user.isEmpty()) {
            JLabel drawUserLabel = new JLabel(shape.user);
            //计算显示位置
            int x = shape.x1-50;
            if (x < 0) x=0;
            int y = shape.y1-16;
            if (y<0) y=0;
            //计算文字适应控件宽高
            Font myFont = new Font("宋体", Font.PLAIN, 15);
            FontMetrics myMetrics = new FontMetrics(myFont) {};
            Rectangle2D boundsOfString = myMetrics.getStringBounds(shape.user, null);
            int width = (int) boundsOfString.getWidth();
            int height = (int) boundsOfString.getHeight();
            drawUserLabel.setFont(myFont);
            drawUserLabel.setBounds(x,y, width+4, height+4);
            drawUserLabel.setOpaque(true);
            drawUserLabel.setBackground(shape.color);
            //名字颜色取反色
            drawUserLabel.setForeground(new Color(255-shape.color.getRed(), 255-shape.color.getGreen(), 255-shape.color.getBlue()));
            panel.add(drawUserLabel);
            panel.repaint();
            new Thread(() -> {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                drawUserLabel.setVisible(false);
            }).start();
        }
    }

    public void clearBoard() {
        shapeList.clear();
        g2d = (Graphics2D)panel.getGraphics();
        g2d.clearRect(0,0,1100,800);
    }
}
